var pageSize = 4;

$(document).ready(function(){
	var $loadMoreQuestions = $("#loadMoreQuestions");
	var $questionList = $("#questionList");
	var $loadQuestionsStartIndex = $("#loadQuestionsStartIndex");
	$loadQuestionsStartIndex.val(0);
	$.mathsetAsynHandler(contextPath + "/question/retrieveQuestions", "startIndex=0", function(data){
		if(data != null || data.length > 0) {
			var allQuestions = '';
			var len = data.length;
			for(var i=0; i<len; i++) {
				if(i == pageSize) {
					break;
				}
				allQuestions += assembleQuestion(data[i]);
			}
			$questionList.append(allQuestions);
			if(len < 5) {
				$loadMoreQuestions.hide();
			}
		} else {
			$loadMoreQuestions.hide();
		}
	});
	
	$loadMoreQuestions.click(function(){
		var startIndex = parseInt($loadQuestionsStartIndex.val()) + pageSize;
		var params = "startIndex=" + startIndex;
		$.mathsetAsynHandler(contextPath + "/question/retrieveQuestions", params, function(data){
			if(data != null || data.length > 0) {
				var allQuestions = '';
				var len = data.length;
				for(var i=0; i<len; i++) {
					if(i == pageSize) {
						break;
					}
					allQuestions += assembleQuestion(data[i]);
				}
				$questionList.append(allQuestions);
				
				if(len < 5) {
					$loadMoreQuestions.hide();
				} else {
					$loadQuestionsStartIndex.val(startIndex);
				}
			}
		});
	});
	
	function assembleQuestion(question) {
		return '<div class="quesHover stage1">'
			+ '<div style="padding-left: 6px;width: 742px;word-break: break-all;">'
			+ '<a href="' + contextPath + '/question/viewQuestion?id='+ question.tid + '">' 
			+ question.content
			+ '</a></div>'
			+ '<div style="width: 724px;height: 50px;text-align: right;line-height: 42px;">'
			+ '<span>'
			+ ''
			+ '</span>，<span>'
			+ question.createTime
			+ '</span></div>'
			+ '<div style="padding-left: 6px;width: 750px;height: 50px;padding-bottom:3px;">'
			+ '<input type="button" value="10个赞" class="baseBtn2" style="background-color: #E768AD;"/>'
			+ '<input type="button" value="100人看过" class="baseBtn2" style="background-color: #6BEC3B;"/>'
			+ '<input type="button" value="5个解" class="baseBtn2" style="background-color: #37B6CE;"/>'
			+ '<input type="button" value="3次有难度" class="baseBtn2" style="background-color: #0E51A7;"/>'
			+ '</div></div>';
	}
	
});